package com.example.opengldemo;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.opengldemo._08_3D.MyView3D;
import com.example.opengldemo._09_OBJ.MyObjFileView;
import com.example.opengldemo._111_.MyTreeView;
import com.example.opengldemo._112_.MyMountionView;

import com.example.opengldemo.fbo.MySurfaceView;
import com.example.opengldemo.mbo.MyEarthAndMoonSystem;
import com.example.opengldemo.mirror.MyMirrorView;
import com.example.opengldemo.sky.Sky;
import com.example.opengldemo.util.MatrixState;

import java.math.BigDecimal;


public class MainActivity_EarthMoonStar extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {

    MyEarthAndMoonSystem system;
    MyView3D myView3D;
    MyObjFileView myObjFileView;
    MySurfaceView mySurfaceView;
    MyTreeView myTreeView;
    MyMountionView myMountionView;
    Sky sky;
    MyMirrorView myMirrorView ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_earth); //跳转到相关界面
        LinearLayout ll = (LinearLayout) findViewById(R.id.main_liner);
        ll.addView(sky());

        findViewById(R.id.camera).setOnClickListener(this);
        findViewById(R.id.trance).setOnClickListener(this);
        findViewById(R.id.camera_t).setOnClickListener(this);
        findViewById(R.id.light).setOnClickListener(this);
        findViewById(R.id.rotate).setOnClickListener(this);
        findViewById(R.id.bt_init).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                x = y =z = 0 ;
                setChange();
            }
        });

        findViewById(R.id.bt_x_add).setOnClickListener(this);
        findViewById(R.id.bt_x_sub).setOnClickListener(this);
        findViewById(R.id.bt_y_add).setOnClickListener(this);
        findViewById(R.id.bt_y_sub).setOnClickListener(this);
        findViewById(R.id.bt_z_add).setOnClickListener(this);
        findViewById(R.id.bt_z_sub).setOnClickListener(this);
        findViewById(R.id.bt_w_add).setOnClickListener(this);
        findViewById(R.id.bt_w_sub).setOnClickListener(this);

        findViewById(R.id.bt_x_add).setOnTouchListener(this);
        findViewById(R.id.bt_x_sub).setOnTouchListener(this);
        findViewById(R.id.bt_y_add).setOnTouchListener(this);
        findViewById(R.id.bt_y_sub).setOnTouchListener(this);
        findViewById(R.id.bt_z_add).setOnTouchListener(this);
        findViewById(R.id.bt_z_sub).setOnTouchListener(this);
        findViewById(R.id.bt_w_add).setOnTouchListener(this);
        findViewById(R.id.bt_w_sub).setOnTouchListener(this);

        SeekBar viewById = findViewById(R.id.SeekBar);
        viewById.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (i > 70) {
                    type = i - 70;
                } else {
                    float a = (float) i / 70;
                    BigDecimal c = new BigDecimal(a);
                    type = c.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity_EarthMoonStar.this, "type >"+type, Toast.LENGTH_SHORT).show();
            }
        });
    }

    volatile float x;
    volatile float y;
    volatile float z;
    volatile float w;
    volatile float type = 0.2f;
    volatile int a;
    final int CAMERA = 1;
    final int CAMERA_T = 4;
    final int CAMERA_UP = 6;
    final int LIGHT = 2;
    final int TRANS = 3;
    final int ROTATE = 5;

    public void setChange() {
        switch (a) {
            case CAMERA: {
                MatrixState.setCamera(x, y, z, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
                break;
            }
            case CAMERA_T: {

                MatrixState.setCamera(MatrixState.cameraLocation[0], MatrixState.cameraLocation[1], MatrixState.cameraLocation[2],
                        x, y, z, 0f, 1.0f, 0.0f);
                break;
            }
            case CAMERA_UP: {
                MatrixState.setCamera(x, y, z, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
                break;
            }
            case LIGHT: {
                MatrixState.setLightDirection(x, y, z);
                break;
            }
            case ROTATE: {
                MatrixState.rotate[0] = x;
                MatrixState.rotate[1] = y;
                MatrixState.rotate[2] = z;
                break;
            }
            case TRANS: {
                MatrixState.translate[0] = x;
                MatrixState.translate[1] = y;
                MatrixState.translate[2] = z;
                break;
            }
        }
        ((TextView) findViewById(R.id.tv_x_value)).setText(("" + x));
        ((TextView) findViewById(R.id.tv_y_value)).setText(("" + y));
        ((TextView) findViewById(R.id.tv_z_value)).setText(("" + z));
        ((TextView) findViewById(R.id.tv_w_value)).setText(("" + w));
    }

    public GLSurfaceView system() {
        system = new MyEarthAndMoonSystem(this); //创建 MyTDView 类的对象
        system.requestFocus(); //获取焦点
        system.setFocusableInTouchMode(true); //设置为可触控
        return system;
    }

    public MyView3D init3D() {
        myView3D = new MyView3D(this); //创建 MyTDView 类的对象
        myView3D.requestFocus(); //获取焦点
        myView3D.setFocusableInTouchMode(true); //设置为可触控
        return myView3D;
    }

    public MyObjFileView myObjFileView() {
        myObjFileView = new MyObjFileView(this); //创建 MyTDView 类的对象
        myObjFileView.requestFocus(); //获取焦点
        myObjFileView.setFocusableInTouchMode(true); //设置为可触控
        return myObjFileView;
    }

    public MySurfaceView mySurfaceView() {
        mySurfaceView = new MySurfaceView(this); //创建 MyTDView 类的对象
        mySurfaceView.requestFocus(); //获取焦点
        mySurfaceView.setFocusableInTouchMode(true); //设置为可触控
        return mySurfaceView;
    }
    public MyMountionView myMountionView() {
        myMountionView = new MyMountionView(this); //创建 MyTDView 类的对象
        myMountionView.requestFocus(); //获取焦点
        myMountionView.setFocusableInTouchMode(true); //设置为可触控
        return myMountionView;
    }
    public Sky sky() {
        sky = new Sky(this); //创建 MyTDView 类的对象
        sky.requestFocus(); //获取焦点
        sky.setFocusableInTouchMode(true); //设置为可触控
        return sky;
    }

    public MyMirrorView myMirrorView() {
        myMirrorView = new MyMirrorView(this); //创建 MyTDView 类的对象
        myMirrorView.requestFocus(); //获取焦点
        myMirrorView.setFocusableInTouchMode(true); //设置为可触控
        return myMirrorView;
    }

    public MyTreeView myTreeView() {
        myTreeView = new MyTreeView(this); //创建 MyTDView 类的对象
        myTreeView.requestFocus(); //获取焦点
        myTreeView.setFocusableInTouchMode(true); //设置为可触控
        return myTreeView;
    }

    public MyTreeView mysurfaceview2() {
        myTreeView = new MyTreeView(this); //创建 MyTDView 类的对象
        myTreeView.requestFocus(); //获取焦点
        myTreeView.setFocusableInTouchMode(true); //设置为可触控
        return myTreeView;
    }
    @Override
    public void onResume() {
        super.onResume();
        if (system != null) system.onResume();
        if (mySurfaceView != null) mySurfaceView.onResume();
        if (myTreeView != null) myTreeView.onResume();
        if (myMountionView != null) myMountionView.onResume();
        if (sky != null) sky.onResume();
        if (myMirrorView != null) myMirrorView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (system != null) system.onPause();
        if (mySurfaceView != null) mySurfaceView.onPause();
        if (myTreeView != null) myTreeView.onPause();
        if (myMountionView != null) myMountionView.onPause();
        if (sky != null) sky.onPause();
        if (myMirrorView != null) myMirrorView.onPause();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.camera:
                a = CAMERA;
                x = MatrixState.cameraLocation[0];
                y = MatrixState.cameraLocation[1];
                z = MatrixState.cameraLocation[2];

                break;
            case R.id.light:
                a = LIGHT;
                x = MatrixState.lightLocation[0];
                y = MatrixState.lightLocation[1];
                z = MatrixState.lightLocation[2];
                break;
            case R.id.rotate:
                x = MatrixState.rotate[0];
                y = MatrixState.rotate[1];
                z = MatrixState.rotate[2];
                a = ROTATE;
                break;
            case R.id.trance:
                x = MatrixState.translate[0];
                y = MatrixState.translate[1];
                z = MatrixState.translate[2];
                a = TRANS;
                break;

            case R.id.camera_t:
                a = CAMERA_T;
                x=y=z=0 ;
                break;

            case R.id.bt_x_add:
                x += type;
                break;
            case R.id.bt_x_sub:
                x -= type;
                break;
            case R.id.bt_y_add:
                y += type;
                break;
            case R.id.bt_y_sub:
                y -= type;
                break;
            case R.id.bt_z_add:
                z += type;
                break;
            case R.id.bt_z_sub:
                z -= type;
                break;
            case R.id.bt_w_add:
                w += type;
                break;
            case R.id.bt_w_sub:
                w -= type;
                break;
        }
        setChange();
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_MOVE:
                onClick(view);
                break;
        }
        return true;
    }
}
